/*
 * <pre>
 * Copyright (c) The Loose Coupling Team.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *</pre>
 */
package org.lc.eic.messagerouter;

import org.lc.eic.core.pipesandfilters.Pipe;
import org.lc.eic.core.pipesandfilters.exception.ProcessingException;
import org.lc.eic.core.support.logging.DefaultLogMessageDescriber;
import org.lc.eic.core.support.logging.DefaultLogger;
import org.lc.eic.core.support.logging.LogMessageDescriber;
import org.lc.eic.core.support.logging.Logger;
import org.lc.eic.messagerouter.exception.FailedRouteMessageException;
import org.lc.eic.messagerouter.exception.NoPipeFoundException;

/**
 * The message-router pattern implementation. It encapsulates the actual routing. The routing is always assumed to be stateless.
 * <br/><br/>
 * The routing conditions and desicions can be very different between different routing situations. 
 * Also the way the outbound-pipes are provided to the message-router may vary alot.
 * To separate the things that can change(conditions, desicions and outbound pipes locations) from the thing that is static(routing) we have defined two interfaces 
 * that is used by this message router implementation:
 * 
 *  <ul>
 *  	<li><b>PipeIdentifier</b>: Responsible for encapsulating the rules(conditions/desicions) for routing and resolving a pipe-identifier as a result from processing of the rules.</li>
 *  	<li><b>PipeProvider</b>: Responsible for providing the outbound-pipes and match them with the pipe-identifier.</li>
 *  </ul>
 *  By having this separation between the routing-logic and the provider of outbound-pipes we are able to create reusable implementations for each.  
 * 
 *	@author <a href="mailto:p.lycke@googlemail.com">PaLy</a>
 *
 * 
 */
public class MessageRouter<I> implements Pipe<I> {
	private Logger logger = new DefaultLogger(MessageRouter.class);
	
	private PipeIdentifier<I> pipeIDResolver;
	private PipeProvider<I> pipeProvider;
	
	private Pipe<I> noDestinationPipe = null;
	
	
	private LogMessageDescriber<I> loggMessageDescriber = new DefaultLogMessageDescriber<I>();

	


	public void process(I i) throws ProcessingException{		
		
		String pipeIdentifier = pipeIDResolver.resolvePipeIdentifier(i);		
		
		if (logger.isDebugEnabled()){
			logger.debug("Message will be routed. [message="+loggMessageDescriber.logMessageDescription(i)+", pipe-id=" + pipeIdentifier+"]");
		}
		
		try{			
			Pipe<I> outboundPipe = pipeProvider.providePipe(pipeIdentifier);
			outboundPipe.process(i);
			
		} catch(NoPipeFoundException npfe){		
		
			if(noDestinationPipe == null){
				throw new FailedRouteMessageException("No destination found. [pipe-id="+pipeIdentifier+"]", npfe);
			} else{
				if (logger.isDebugEnabled()){
					logger.debug("No outpubnd pipe found for pipe-id = '" + 
							pipeIdentifier + "', using the No Default handler.");
				}
				noDestinationPipe.process(i);
			}
		}
	}


	public void setPipeIdentifier(PipeIdentifier<I> pipeIdentifier) {
		this.pipeIDResolver = pipeIdentifier;
	}


	public void setPipeProvider(PipeProvider<I> pipeProvider) {
		this.pipeProvider = pipeProvider;
	}

	/**
	 * Inject a pipe when you want special handling for dead letter messages, like place it on a dead letter queue.
	 * @param noDestinationPipe
	 */
	public void setNoDestinationPipe(Pipe<I> noDestinationPipe) {
		this.noDestinationPipe = noDestinationPipe;
	}
	
	public void setLoggMessageDescriber(LogMessageDescriber<I> loggMessageDescriber) {
		this.loggMessageDescriber = loggMessageDescriber;
	}


	public void setLogger(Logger logger) {
		this.logger = logger;
	}


}
